const express = require('express')
const ChatController = require('../controllers/chatController')
const { validateChatSessionCreate, validateChatSessionUpdate, validateChatMessageSend, validateChatMessageEdit, validateChatMessageAddReaction, validateChatSessionAddTag, validatePagination, validateSearch } = require('../middleware/validation')
const { authenticateToken, requireSessionOwnership } = require('../middleware/auth')
const { aiLimiter } = require('../middleware/security')

const router = express.Router()

// 创建聊天会话
router.post('/sessions',
  authenticateToken,
  validateChatSessionCreate,
  ChatController.createSession
)

// 获取用户的聊天会话列表
router.get('/sessions',
  authenticateToken,
  validatePagination,
  ChatController.getUserSessions
)

// 获取聊天会话详情
router.get('/sessions/:sessionId',
  authenticateToken,
  requireSessionOwnership,
  ChatController.getSession
)

// 更新聊天会话
router.put('/sessions/:sessionId',
  authenticateToken,
  requireSessionOwnership,
  validateChatSessionUpdate,
  ChatController.updateSession
)

// 删除聊天会话
router.delete('/sessions/:sessionId',
  authenticateToken,
  requireSessionOwnership,
  ChatController.deleteSession
)

// 恢复已删除的会话
router.put('/sessions/:sessionId/restore',
  authenticateToken,
  requireSessionOwnership,
  ChatController.restoreSession
)

// 发送消息
router.post('/sessions/:sessionId/messages',
  authenticateToken,
  requireSessionOwnership,
  aiLimiter,
  validateChatMessageSend,
  ChatController.sendMessage
)

// 获取会话消息列表
router.get('/sessions/:sessionId/messages',
  authenticateToken,
  requireSessionOwnership,
  validatePagination,
  ChatController.getSessionMessages
)

// 编辑消息
router.put('/sessions/:sessionId/messages/:messageId',
  authenticateToken,
  requireSessionOwnership,
  validateChatMessageEdit,
  ChatController.editMessage
)

// 删除消息
router.delete('/sessions/:sessionId/messages/:messageId',
  authenticateToken,
  requireSessionOwnership,
  ChatController.deleteMessage
)

// 添加消息反应
router.post('/sessions/:sessionId/messages/:messageId/reactions',
  authenticateToken,
  requireSessionOwnership,
  validateChatMessageAddReaction,
  ChatController.addMessageReaction
)

// 移除消息反应
router.delete('/sessions/:sessionId/messages/:messageId/reactions',
  authenticateToken,
  requireSessionOwnership,
  validateChatMessageAddReaction,
  ChatController.removeMessageReaction
)

// 搜索消息
router.get('/messages/search',
  authenticateToken,
  validateSearch,
  ChatController.searchMessages
)

// 导出会话
router.get('/sessions/:sessionId/export',
  authenticateToken,
  requireSessionOwnership,
  ChatController.exportSession
)

// 批量操作会话
router.post('/sessions/batch',
  authenticateToken,
  ChatController.batchUpdateSessions
)

// 获取会话统计信息
router.get('/sessions/:sessionId/stats',
  authenticateToken,
  requireSessionOwnership,
  ChatController.getSessionStats
)

// 添加会话标签
router.post('/sessions/:sessionId/tags',
  authenticateToken,
  requireSessionOwnership,
  validateChatSessionAddTag,
  ChatController.addSessionTag
)

// 移除会话标签
router.delete('/sessions/:sessionId/tags',
  authenticateToken,
  requireSessionOwnership,
  validateChatSessionAddTag,
  ChatController.removeSessionTag
)

// 切换会话置顶状态
router.put('/sessions/:sessionId/pin',
  authenticateToken,
  requireSessionOwnership,
  ChatController.toggleSessionPin
)

module.exports = router